package com.ambulance.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/***
 *  @description
 *  @author zhao
 *  @date: 2020/4/15
 *  @version: v1.0
 */
public class CommonUtils {


    public static void main(String[] args) {

        System.out.println(getRandomString());

    }

    public static String parseLocalDateTime(LocalDateTime localDateTime) {
        String format = "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }


    public static String getRandomNumber() {
        String trandStr = String.valueOf((Math.random() * 9 + 1) * 10000);
        String numStr = trandStr.toString().substring(0, 5);
        return numStr;
    }

    /**
     * 获取随机字符串---构建流水号
     *
     * @return
     */
    public static String getRandomString() {
        UUID uuid = UUID.randomUUID();
        String uuidString = uuid.toString().replace("-", "");
        return parseDateTime() + uuidString.substring(0,14);
    }

    /**
     * 判断字符串是否为
     *
     * @param obj
     * @return
     */
    public static boolean checkNull(Object obj) {

        if (Objects.isNull(obj)) {
            return true;
        }
        if (obj instanceof String) { //str 字符串类型
            return ((String) obj).trim().isEmpty();
        }

        if (obj instanceof List) { //集合
            return ((List) obj).isEmpty();
        }
        //返回非空
        return false;
    }


    /**
     * 字符串转时间类型
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parseStr2LongDateTime(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (!checkNull(str)) {
            Date parse = simpleDateFormat.parse(str);
            return parse;
        }
        return null;
    }

    /**
     * 时间转字符串
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String parseDateTime2Str(Date date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        return format;
    }

    public static String parseDateTime() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(date);
        return format;
    }


}
